//
//  UIImage+image.m
//  图片圆角(贝塞尔曲线)
//
//  Created by 李松 on 2017/11/23.
//  Copyright © 2017年 李松. All rights reserved.
//

#import "UIImage+image.h"

@implementation UIImage (image)

/*
 0.加载图片;
 1.确定边框宽度;
 2.开启一个上下文;
 3.绘制大图显示出来;
 4.绘制一个小圆,设置小圆为裁剪区域;
 5.把图片绘制在上下文中;
 6.从上下文中取出图片;
 7.关闭上下文;
 */

+ (UIImage * _Nonnull)imageWithBorderW:(CGFloat)borderW color:(nonnull UIColor *)color image:(nonnull UIImage *)image{
    
    CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW);
    UIGraphicsBeginImageContext(size);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
    [color set];
    [path fill];
    
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
    [clipPath addClip];
    
    [image drawAtPoint:CGPointMake(borderW, borderW)];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndPDFContext();
    
    return newImage;
}

@end
